package cn.kgc.fcm.dsbank.controller;

import cn.kgc.fcm.dsbank.dto.ResultDTO;
import cn.kgc.fcm.dsbank.entity.Employee;
import cn.kgc.fcm.dsbank.entity.Role;
import cn.kgc.fcm.dsbank.exception.SysException;
import cn.kgc.fcm.dsbank.service.EmpService;
import cn.kgc.fcm.dsbank.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmpController extends BaseController{

    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    @Autowired
    private RoleService roleService;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @RequestMapping("/show_emp")
    public String showEmp(Model model){
        ResultDTO<List<Employee>> empAllDTO = empService.selectEmpAll();
        model.addAttribute("result",empAllDTO);
        return "emp/show_emp";
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @RequestMapping("/show_emp/{empId}")
    public String showEmp(@PathVariable Integer empId, Model model){
        ResultDTO<Employee> employeeResultDTO = empService.selectEmpById(empId);
        model.addAttribute("result",employeeResultDTO);
        return "emp/show_one_emp";
    }

    @RequestMapping("/login")
    public String login(){
        return "/emp/login";
    }



    @RequestMapping("/add_emp")
    public String addEmp(Model model){

        List<Role> roles = roleService.selectRoleAll();

        model.addAttribute("roles",roles);

        return "/emp/add_emp";
    }

    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    @RequestMapping(path = "/do_add_emp",method = RequestMethod.POST)
    public String doAddEmp(Model model, @Valid Employee emp, BindingResult errors, @RequestParam("roles") Integer[] roles) throws SysException {

        if(errors.hasErrors()){

            List<FieldError> list = errors.getFieldErrors();
            for (FieldError fieldErr: list) {
                String name = fieldErr.getField();
                String msg = fieldErr.getDefaultMessage();
                System.out.println(name+"\t"+msg);
                throw new SysException(msg);
            }
        }
        emp.setCreateEmpId(this.getLoginedEmp().getEmpId());

        ResultDTO resultDTO = empService.addEmp(emp, roles);

        model.addAttribute("result",resultDTO);
        return "redirect:/emp/show_emp";
    }
}
